using System.Collections.Generic;
using System;

namespace PropertySystem
{
    [Serializable]
    public class ValueModifier
    {
        private Dictionary<string, float> _baseAdd;
        private Dictionary<string, float> _percentage;
        private Dictionary<string, float> _addition;
        private float _value;
        public float Value { get => _value; private set => _value = value; }

        public ValueModifier()
        {
            _baseAdd = new Dictionary<string, float>();
            _percentage = new Dictionary<string, float>();
            _addition = new Dictionary<string, float>();
            Value = 0;
        }

        public void Register(string key, float value, ModifierType modifierType)
        {
            switch (modifierType)
            {
                case ModifierType.BaseAdd:
                    if (_baseAdd.ContainsKey(key)) return;
                    _baseAdd.Add(key, value);
                    break;
                case ModifierType.Percentage:
                    if (_percentage.ContainsKey(key)) return;
                    _percentage.Add(key, value);
                    break;
                case ModifierType.Addition:
                    if (_addition.ContainsKey(key)) return;
                    _addition.Add(key, value);
                    break;
                default:
                    break;
            }
            RefreshValue();
        }

        public void Unregister(string key)
        {
            if (_baseAdd.ContainsKey(key)) _baseAdd.Remove(key);
            if (_percentage.ContainsKey(key)) _percentage.Remove(key);
            if (_addition.ContainsKey(key)) _addition.Remove(key);
            RefreshValue();
        }

        public bool CheckKey(string key)
        {
            return _baseAdd.ContainsKey(key) || _percentage.ContainsKey(key) || _addition.ContainsKey(key);
        }

        public void RefreshValue()
        {
            float result = 0;
            foreach (var item in _baseAdd) { result += item.Value; }
            float percent = 100f;
            foreach (var item in _percentage) { percent += item.Value; }
            result = percent / 100f * result;
            foreach (var item in _addition) { result += item.Value; }
            Value = result;
        }
    }
}

